package app.beit.fintrack.service;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import app.beit.fintrack.bean.HospitalBean;
import app.beit.fintrack.bean.ProvinceBean;
import app.beit.fintrack.bean.ZoneBean;
import app.beit.fintrack.dao.MasterDao;
import app.beit.fintrack.entity.Hospital;
import app.beit.fintrack.entity.Province;
import app.beit.fintrack.entity.Zone;
import app.beit.fintrack.service.convert.HospitalConvert;
import app.beit.fintrack.service.convert.ProvinceConvert;
import app.beit.fintrack.service.convert.ZoneConvert;

public class MasterService {
	
	private final Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private MasterDao masterDao;
	
	public List<ZoneBean> getZone() throws Exception{
		this.log.info("get zones");
		final List<Zone> entities = this.masterDao.getZone();
		if(entities != null && entities.size() > 0){
			return ZoneConvert.entityToBean(entities);
		}
		
		return null;
	}
	
	public ZoneBean getZoneByZoneId(final String zoneId) throws Exception{
		this.log.info("get zones by zone id");
		final Zone entity = this.masterDao.getZoneByZoneId(zoneId);
		if(entity != null){
			return ZoneConvert.entityToBean(entity);
		}
		
		return null;
	}
	
	public List<HospitalBean> getHospitalByZoneIdAndProvinceId(final String zoneId, final String provinceId) throws Exception{
		this.log.info("get hospital by zone and province");
		final List<Hospital> entities = this.masterDao.getHospitalByZoneIdAndProvinceId(zoneId, provinceId);
		if(entities != null && entities.size() > 0){
			return HospitalConvert.entityToBean(entities);
		}
		return null;
	}
	
	public List<HospitalBean> getAllHospital() throws Exception{
		this.log.info("get all hospital");
		final List<Hospital> entities = this.masterDao.getAllHospital();
		if(entities != null && entities.size() > 0){
			return HospitalConvert.entityToBean(entities);
		}
		return null;
	}
	
	public List<ProvinceBean> getProvinceByZoneId(final String zoneId) throws Exception{
		this.log.info("get province by zone");
		final List<Province> entities = this.masterDao.getProvinceByZoneId(zoneId);
		if(entities != null && entities.size() > 0){
			return ProvinceConvert.entityToBean(entities);
		}
		return null;
	}
	
	public HospitalBean getHospitalByHCode(final String hCode) throws Exception {
		this.log.info("get hospital by hospital code");
		final Hospital entity = this.masterDao.getHospitalByHCode(hCode);
		if(entity != null){
			return HospitalConvert.entityToBean(entity);
		}
		return null;
	}

	public ProvinceBean getProvinceByProvinceId(final String provinceId) throws Exception{
		this.log.info("get province by province id");
		final Province entity = this.masterDao.getProvinceByProvinceId(provinceId);
		if(entity != null){
			return ProvinceConvert.entityToBean(entity);
		}
		return null;
	}
	
	public ProvinceBean getProvinceByHCode(final String hospitalCode) throws Exception {
		this.log.info("get province by hospital id");
		final Province entity = this.masterDao.getProvinceByHCode(hospitalCode);
		if(entity != null){
			return ProvinceConvert.entityToBean(entity);
		}
		return null;
	}
	
	public ZoneBean getZoneByHCode(final String hospitalCode) throws Exception {
		this.log.info("get zone by hospital id");
		final Zone entity = this.masterDao.getZoneByHCode(hospitalCode);
		if(entity != null){
			return ZoneConvert.entityToBean(entity);
		}
		return null;
	}
}
